package com.tailyou.androidlearning202400

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.tailyou.androidlearning202400.util.ToastUtil

class Activity2 : AppCompatActivity() {

    private lateinit var btnBack: Button
    private lateinit var edtParams2: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
//        接受Activity1传递过来的参数
        val params1 = intent.extras?.getString("params1")
        ToastUtil.showToast(this, params1)

        btnBack = findViewById(R.id.btnBack)
        edtParams2 = findViewById(R.id.edtParams2)
        btnBack.setOnClickListener {
            // Activity2往Activity1回传参数
            val intent = Intent()
            intent.putExtra("params2", edtParams2.text.toString())
            setResult(RESULT_OK, intent)
            finish()
        }

    }
}